﻿using Newtonsoft.Json;
using RMS.BLL;
using RMS.EF.Model;
using RMS.EF.Model.Common;
using RMS.Manage.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace RMS.Manage.Web.Areas.Index.Controllers
{
    public class DefaultController : BaseController
    {
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        [RMSAuthorize]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 导航框
        /// </summary>
        /// <returns></returns>
        [RMSAuthorize]
        public ActionResult Navbar()
        {
            ViewBag.UserInfo = LoginUser;
            return PartialView(Menu);
        }
        /// <summary>
        /// 登录页
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            FormsAuthentication.SignOut();
            return View();
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(RMS_User user)
        {
            CommonOutPutVm result = new CommonOutPutVm();
            RMS_User loginUser = null;
            try
            {
                #region 登陆并返回用户信息
                var ubll = new UserBll();
                var result_l = ubll.Login(user);
                if (!result_l.IsSuccess)
                {
                    return Json(result_l);
                }
                loginUser = result_l.Data;
                #endregion
                #region 票据Forms初始化 config文件加authentication配置节
                var userDataJson = JsonConvert.SerializeObject(loginUser);
                DateTime issueDate = DateTime.Now;
                DateTime expiration = issueDate.AddMinutes(60);
                FormsAuthentication.Initialize();
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, loginUser.Id.ToString(), issueDate, expiration, false, userDataJson);
                string enyTicket = FormsAuthentication.Encrypt(ticket);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, enyTicket);
                cookie.Expires = expiration;
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
                #endregion
                Menu = null;
                result.IsSuccess = true;
                result.Id = loginUser.Id;
                return Json(result);

            }
            catch (Exception e)
            {
                result.Message = e.Message;
                return Json(result);
            }
        }
        /// <summary>
        /// 修改密码页
        /// </summary>
        /// <returns></returns>
        public ActionResult EditPassword()
        {
            if (!IsLogin)
            {
                return RedirectToAction("Login");
            }
            return PartialView();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="orgPwd">原始密码</param>
        /// <param name="newPwd">新密码</param>
        /// <param name="rePwd">确认密码</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditPassword(string orgPwd, string newPwd, string rePwd)
        {
            var result = new CommonOutPutVm();
            var ubll = new UserBll();
            result = ubll.EditPassword(UserId, orgPwd, newPwd, rePwd);
            return Json(result);
        }
	}
}